var that
var interval_obj
export default {
    data () {
        return this.datainit({
            list: [],
            page: 1,
            loading: false,
            loaded: false,
            sort: 0,
            id: null,
            search:null
        })
    },

    mounted: function () {
        this.id = this.$route.query.id
        this.search=this.$route.query.search
        that = this
        that.showlist()
        interval_obj=setInterval(function () {
            that.setendtime();
        },1000)
    },
    beforeDestroy:function(){
        clearInterval(interval_obj)
    },
    methods: {
        onScroll(e) {
            let outerHeight = this.$el.querySelector('#list').clientHeight
            let innerHeight = this.$el.clientHeight
            let scrollTop = this.$el.scrollTop
            if (outerHeight - innerHeight - scrollTop < 50) {
                that.loadmore()
            }

        },
        setsort:function(i){
            that.sort=i
            that.showlist()
        },
        showlist: function () {
            this.page = 1;
            this.loading = false;
            this.loaded = false
            this.list=[]
            this.loadmore()
        },
        loadmore: function () {
            if (that.loading || that.loaded) {
                return;
            }
            that.loading = true
            this.$post("api/goods/lists", {cate_id: this.id,search:this.search, page: this.page, sort: this.sort}, function (_list) {
                let current_time=Date.parse(new Date())/1000;
                for(let i=0;i<_list.length;i++){
                    if(_list[i].golist.length>0){
                        for(let j=0;j<_list[i].golist.length;j++){
                            _list[i].golist[j].endtime_str=that.setendtime_core(_list[i].golist[j]['end_timestamp'],current_time)
                        }
                    }
                }
                for (let i in _list) {
                    that.list.push(_list[i])
                }
                if (_list.length < 10) {
                    that.loaded = true
                }
                that.$nextTick(function () {
                    that.page++
                    that.loading = false
                })
            })


        },
        setendtime:function () {
            let current_time=Date.parse(new Date())/1000;
            //console.log(current_time)
            let list=that.list
            for(let i=0;i<list.length;i++){
                if(list[i].golist.length>0){
                    for(let j=0;j<list[i].golist.length;j++){
                        list[i].golist[j].endtime_str=that.setendtime_core(list[i].golist[j]['end_timestamp'],current_time)
                    }
                }
            }
        },
        setendtime_core:function (end_timestamp,current_time) {
            let time=end_timestamp-current_time;
            if(time<0){
                return "已结束"
            }
            let day = Math.floor(time / (60 * 60 * 24));
            let hour = Math.floor(time / (60 * 60)) - (day * 24);
            let minute = Math.floor(time / 60) - (day * 24 * 60) - (hour * 60);
            let second = Math.floor(time) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            if (day <= 9) day = '0' + day;
            if (hour <= 9) hour = '0' + hour;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            return "剩:<span>"+day+"</span>天<span>"+hour+"</span>:<span>"+minute+"</span>:<span>"+second+"</span>"
        }
    },


}